﻿/// <reference path="~/ExtClient/ext/ext-all-debug-w-comments.js" />
Ext.define('app.controller.Login', {
    extend: 'Ext.app.Controller',

    views: ['loginwin'],
    models: ['User'],
    stores: ['User'],

    refs:
        [{
            ref: 'loginForm',
            selector: '.form'
        }, {
            ref: 'loginButton',
            selector: 'loginwin button[action=login]'
        }, {
            ref: 'resetButton',
            selector: 'loginwin button[action=reset]'
        }, {
            ref: 'loginWin',                                        //getLowinWin()获取对loginwin组件的引用，不能用getLoginwinView或者getViews('loginwin')，因为这两个方法返回的都是类，而不是实例
            selector: 'loginwin'
        }, {
            ref: 'userName',
            selector: 'loginwin textfield[name = UserName]'
        }
        ],

    init: function () {
        var me = this;

        Ext.EventManager.onWindowResize(function () {                   //监听浏览器窗口的Resize事件
            me.getLoginWin().center();
        });

        this.control({
            'loginwin button[action=login]': {
                click: me.loginclick
            },
            'loginwin button[action=reset]': {
                click: function () {
                    me.getLoginForm().form.reset();
                    me.getUserName().focus(false, 100);
                }
            },
            'loginwin': {
                show: function (win, eOpts) {
                    var keyMap = new Ext.KeyMap(win.getEl(), {
                        key: Ext.EventObject.ENTER,
                        fn: me.loginclick
                    });
                    me.getUserName().focus(false, 100);
                }
            },



            'loginwin button[name=Test]': {
                click: function () {
                    var flag = 'C';
                    switch (flag) {
                        case 'C':
                            me.getUserStore().add({ UserName: 'user1', Email: '111@ee.com' });
                            me.getUserStore().add({ UserName: 'user2', Email: '222@ee.com' });
                            me.getUserStore().add({ UserName: 'user3', Email: '333@ee.com' });
                            me.getUserStore().add({ UserName: 'user4', Email: '444@ee.com' });
                            me.getUserStore().add({ UserName: 'user5', Email: '444@ee.com' });
                            me.getUserStore().add({ UserName: 'user6', Email: '444@ee.com' });

                            console.log('Add records:'+me.getUserStore().getCount());

                            me.getUserStore().getAt(0).phantom = true;                          //由于idgen的原因所以需要手工设置为true
                            me.getUserStore().getAt(1).phantom = true;

                            me.getUserStore().getAt(2).setDirty();                              //手工设置更新状态
                            me.getUserStore().getAt(3).setDirty();

                            me.getUserStore().removeAt(5);                                      //删除
                            me.getUserStore().removeAt(4);

                            console.log('before sync:' + me.getUserStore().getCount());

                            me.getUserStore().sync({
                                scope :this,
                                success: function (batch, operation) {
                                    console.log('after sync success:' + me.getUserStore().getCount());
                                },
                                callback: function (batch, operation) {
                                    console.log('after sync callback:' + me.getUserStore().getCount());
                                },
                                failure: function (batch, operation) {
                                    console.log('after sync failure:' + me.getUserStore().getCount());
                                }
                            });                            
                            
                            //obj[0].save({
                            //    success: function (record, operation) {
                            //        var txt = operation.response.responseText;
                            //        console.log(txt);
                            //        var o = Ext.decode(txt);
                            //        console.log(o.user.ID);
                            //        //record.data.ID = o.user.ID;
                            //        var old = me.getUserStore().getById(record.get('ID'));
                            //        console.log('client object Id:'+old.get('ID'));
                            //        console.log('client object UserName:' + old.get('UserName'));
                            //    }
                            //});
                            //console.log(obj[0].get('UserName'));


                            //var newObj = Ext.create('app.model.User', {
                            //    UserName: 'Added',
                            //    Email: '2222@ee.com'
                            //});
                            //newObj.phantom = true;
                            //newObj.save({
                            //    success: function (record, operation) {
                            //        console.log('client object Id:' + newObj.get('ID'));
                            //        console.log('client object UserName:' + newObj.get('UserName'));
                            //    }
                            //});                            

                            break;
                        default:
                            me.getUserModel().load('1');
                            var obj = me.getUserStore().getById('1');
                            if (obj != null) {
                                console.log('getById is ok');
                            }
                            else {
                                console.log('getById is not ok');
                            }
                            var id = '1';
                            me.getUserStore().load({
                                id: id, //set the id here，当然也可以直接给个常量
                                scope: this,
                                callback: function (records, operation, success) {
                                    if (success) {
                                        for (var i = 0; i < records.length; i++) {
                                            console.log(records[i].get('UserName'));
                                        }
                                        console.log(me.getUserStore().getCount());
                                    }
                                }
                            });
                            break;
                    }
                }
            }
        });
    },

    loginclick: function (button) {
        var me = this;
        if (!this.getLoginForm().form.isValid()) {
            return;
        }
        me.getLoginForm().form.submit({
            clientValidation: true,
            waitMsg: '请等待...',
            waitTitle: '发送请求中',
            url: '/Account/Login',
            method: 'POST',
            success: function (form, action) {
                Ext.Msg.alert(action.result.msg);
            },
            failure: function (form, action) {
                me.getUserName().focus(false, 100);
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.Msg.alert('错误', '请填写登录信息');
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('错误', '与服务器通讯失败');
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        Ext.Msg.alert('错误', action.result.msg);
                }
            }
        });
    }
});